package edu.uit.se.slgroup.construct.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import edu.uit.se.slgroup.construct.domain.Category;
import edu.uit.se.slgroup.construct.domain.SubCategory;
import edu.uit.se.slgroup.construct.domain.SubCategoryTemp;
import edu.uit.se.slgroup.construct.services.CategoryService;
import edu.uit.se.slgroup.construct.services.SubCategoryService;

@Controller
public class SubCategoryController {

	@Autowired
	SubCategoryService subCategoryService;
	
	@Autowired
	CategoryService categoryService;
	
	@RequestMapping("insert-subcategory-form")
	public ModelAndView getForm(@ModelAttribute SubCategory subCategory) {
		List<Category> categoryList = categoryService.getCategoryList();
		for (Category category : categoryList) {
			category.getName();
		}
		System.out.println("get form insert subcategory");
		return new ModelAndView("insert-subcategory", "categoryList", categoryList);	//insert-subcategory.jsp			
	}
	
	@RequestMapping("insertSubCategory")
	public ModelAndView insertSubCategory(@ModelAttribute SubCategory subCategory) {
		subCategoryService.insertSubCategory(subCategory);
		return new ModelAndView("redirect:subcategorytemp-list-form");
	}
	
	@RequestMapping("subcategory-list-form")
	public ModelAndView getSubCategoryList() {
		List<SubCategory> subCategoryList = subCategoryService.getSubCategoryList();
		System.out.println(" in get subcategory list");
		for (SubCategory subCategory : subCategoryList) {
			System.out.println(subCategory.getName());
		}
		return new ModelAndView("subcategory-list", "subCategoryList", subCategoryList);		//subcategory-list.jsp
	}
	
	@RequestMapping("subcategorytemp-list-form")
	public ModelAndView getSubCategoryTempList() {
		List<SubCategoryTemp> subCategoryList = subCategoryService.getSubCategoryTempList();
		System.out.println(" in get subcategory list");
//		for (SubCategory subCategory : subCategoryList) {
//			System.out.println(subCategory.getName());
//		}
		return new ModelAndView("subcategory-list", "subCategoryList", subCategoryList);		//subcategory-list.jsp
	}
	
	@RequestMapping("deleteSubCategory")
	public ModelAndView deleteSubCategory(@RequestParam int id) {
		subCategoryService.deleteSubCategory(id);
		return new ModelAndView("redirect:subcategorytemp-list-form");
	}
	
	@RequestMapping("edit-subcategory-form")
	public ModelAndView editSubCategory(@RequestParam int id, @ModelAttribute SubCategory subCategory) {
		SubCategory subCategoryObject = subCategoryService.getSubCategoryById(id);
		
		List<Category> categoryList = categoryService.getCategoryList();
		for (Category category : categoryList) {
			category.getName();
		}
		
		ModelAndView mv = new ModelAndView("edit-subcategory");		//edit-subcategory.jsp
		mv.addObject("subCategoryObject", subCategoryObject);
		mv.addObject("categoryList", categoryList);
		//return new ModelAndView("edit-subcategory", "subCategoryObject", subCategoryObject);
		return mv;
	}
	
	@RequestMapping("updateSubCategory")
	public ModelAndView updateSubCategory(@ModelAttribute SubCategory subCategory) {
		subCategoryService.updateSubCategory(subCategory);
		return new ModelAndView("redirect:subcategorytemp-list-form");
	}
}
